const app = getApp()
import { getMyProject } from '../../../../api/index'
import { formatTimeTwo } from '../../../../utils/util'

Page({
  data: {
    safeBottom: 0,
    teamTabH: 0,
    tabActive: 'person',
    teamActive: 'myCreatd',
    personalProject: [],
    companyProjectSelf: [],
    companyProjectJoin: [],
    list: [],
    refresher: false,
    showHandlesPop: false,
    currentItem: {}
  },
  showHandles ({ detail }) {
    this.setData({ showHandlesPop: detail.show, currentItem: detail.item })
  },
  cancel () {
    this.setData({ showHandlesPop: false, currentItem: {} })
  },
  async onRefresh () {
    await this.getList()
    this.setData({ refresher: false })
  },
  changeTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ tabActive: value })
    if (value === 'team') {
      this.setData({ list: this.data.companyProjectSelf })
      let query = this.createSelectorQuery()
      query.select('.team-tabs').boundingClientRect(rect=>{
        let height = rect.height
        this.setData({ teamTabH: height + 10 })
      }).exec()
    } else {
      this.setData({ list: this.data.personalProject, teamTabH: 0 })
    }
  },
  changeTeamTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ teamActive: value })
    if (value === 'myCreatd') {
      this.setData({ list: this.data.companyProjectSelf })
    } else {
      this.setData({ list: this.data.companyProjectJoin })
    }
  },
  addProject () {
    wx.$nav.navigateTo(`/pages/account/project/edit/index`)
  },
  changeList () {
    this.cancel()
    this.getList()
  },
  async getList () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getMyProject()
      ResultSet.userData.personalProject.forEach(item => {
        item.createDate = formatTimeTwo(item.createDate, 'Y-M-D')
      })
      ResultSet.userData.companyProjectSelf.forEach(item => {
        item.createDate = formatTimeTwo(item.createDate, 'Y-M-D')
      })
      ResultSet.userData.companyProjectJoin.forEach(item => {
        item.createDate = formatTimeTwo(item.createDate, 'Y-M-D')
      })
      this.setData({
        personalProject: ResultSet.userData.personalProject,
        companyProjectSelf: ResultSet.userData.companyProjectSelf,
        companyProjectJoin: ResultSet.userData.companyProjectJoin
      })
      if (this.data.tabActive === 'person') {
        this.setData({ list: ResultSet.userData.personalProject })
      } else {
        if (this.data.teamActive === 'myCreatd') {
          this.setData({ list: ResultSet.userData.companyProjectSelf })
        } else {
          this.setData({ list: ResultSet.userData.companyProjectJoin })
        }
      }
      wx.hideLoading()
    } catch (err) {
      wx.hideLoading()
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad () {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    this.setData({ safeBottom: screenHeight - bottom })
    this.getList()
  }
})